﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TanmiaGrp.GeneralLedger.Core
{
    public class VoucherLine : IDataErrorInfo
    {
        public int VoucherLineID { get; set; }
        public double Credit { get; set; }
        public double Debit { get; set; }
        public string Description { get; set; }
        public Account Account { get; set; }
        public int AccountID { get; set; }

        #region IDataErrorInfo Members

        public string Error
        {
            get
            {
                StringBuilder error = new StringBuilder();

                // iterate over all properties of this object
                // aggregating any validation errors.

                PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this);
                foreach (PropertyDescriptor prop in props)
                {
                    string propertyError = this[prop.Name];
                    if (!string.IsNullOrEmpty(propertyError))
                    {
                        error.AppendFormat("{0}{1}", error.Length != 0 ? ", " : "", propertyError);
                    }
                }

                // apply object level validation rules

                if (Credit > 0 && Debit > 0)
                    error.AppendFormat("{0}{1}", error.Length != 0 ? ", " : "", "لا يمكن تدوين طريفي القيد");

                return error.ToString();
            }
        }

        public string this[string columnName]
        {
            get
            {
                if (columnName == "Credit")
                {
                    if (Credit < 0)
                        return "الدائن يجب أن يكون أكبر من الصفر";
                }

                if (columnName == "Debit")
                {
                    if (Debit < 0)
                        return "المدين يجب أن يكون أكبر من الصفر";
                }

                if (columnName == "AccountID")
                {
                    if (AccountID <= 0)
                        return "يجب تحديد الحساب";
                }

                if (columnName == "Description")
                {
                    if (string.IsNullOrWhiteSpace(Description))
                        return "يجب كتابة وصف للقيد";
                }

                return string.Empty;
            }
        }

        #endregion IDataErrorInfo Members
    }
}
